package ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseFragment;
import com.fy.fyzf.bean.AreaListBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.CommonSearchBean;
import com.fy.fyzf.bean.DemandListBean;
import com.fy.fyzf.bean.DemanddetailBean;
import com.fy.fyzf.bean.IsInfoBean;
import com.fy.fyzf.bean.MyRecommendBean;
import com.fy.fyzf.bean.ReleaseBean;
import com.fy.fyzf.dialog.DeleteWarningDialog;
import com.fy.fyzf.presenter.DemandPresenter;
import com.fy.fyzf.utils.DateUtils;
import com.fy.fyzf.view.DemandCallBack;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import ui.activity.DemandDetailActivity;
import ui.activity.IssueDemandActivity;
import ui.activity.MyDemandDetailActivity;
import ui.adapter.MydemandAdapter;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyDemandFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyDemandFragment extends BaseFragment<DemandPresenter> implements DemandCallBack {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private MydemandAdapter mydemandAdapter;

    public MyDemandFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MyDemandFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MyDemandFragment newInstance(String param1, String param2) {
        MyDemandFragment fragment = new MyDemandFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    protected DemandPresenter createPresenter() {
        return new DemandPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_my_demand;
    }
    private int page = 1;
    private int pageSize = 10;
    private int tag = 1;
    private CommonSearchBean commonSearchBean = new CommonSearchBean();
    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        registerEventBus(this);
        mydemandAdapter = new MydemandAdapter();
        recycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycleView.setAdapter(mydemandAdapter);

        commonSearchBean.setPageNum(page);
        commonSearchBean.setPageSize(pageSize);

        mydemandAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<DemandListBean.ListBean> data = adapter.getData();
                Intent intent = new Intent(getActivity(), MyDemandDetailActivity.class);
                intent.putExtra("data", data.get(position));
                intent.putExtra("type",2);
                startActivity(intent);
            }
        });
        mydemandAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<DemandListBean.ListBean> listBeans = adapter.getData();
                if (view.getId() == R.id.tv_edit) {
                    Intent intent = new Intent(getActivity(), IssueDemandActivity.class);
                    intent.putExtra("data", listBeans.get(position));
                    startActivity(intent);
                } else if (view.getId() == R.id.tv_delete){
                    showDeleteDialog(position, listBeans.get(position).getReleaseId());
                }
            }


        });

        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                tag = 2;
                page++;
                commonSearchBean.setPageNum(page);
                commonSearchBean.setPageSize(pageSize);
                loadData();
            }
        });

        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                tag = 1;
                page = 1;
                commonSearchBean.setPageNum(page);
                commonSearchBean.setPageSize(pageSize);
                loadData();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unregisterEventBus(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String s){
        if (s.equals("issueSuccess")) {
            loadData();
        }
    }

    private ReleaseBean releaseBean = new ReleaseBean();
    private void showDeleteDialog(int i,int id) {
        DeleteWarningDialog deleteWarningDialog = new DeleteWarningDialog(getActivity());
        deleteWarningDialog.setTitle("确定关闭该需求？");
        deleteWarningDialog.setContent("关闭该需求后\n" +
                "对方将无法看到您推荐的房源 !\n");
        deleteWarningDialog.toggleDialog();
        deleteWarningDialog.getView(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteWarningDialog.toggleDialog();
                releaseBean.setReleaseId(id);
                mPresenter.deleteDemand(releaseBean,i);
            }
        });

    }
    @Override
    protected void loadData() {
        mPresenter.getMyDemandList(commonSearchBean);
    }

    @Override
    public void getDemandListSuccess(DemandListBean demandListBean) {
    }

    @Override
    public void issueDemandSuccess(BaseData data) {

    }

    @Override
    public void getAreaListSuccess(List<AreaListBean> listBeans) {

    }

    @Override
    public void getDamendDetailSuccess(DemanddetailBean demanddetailBean) {

    }

    @Override
    public void getMyDemandListSuccess(DemandListBean demandListBean) {
        smartRefresh.finishLoadMore();
        smartRefresh.finishRefresh();
        if (null == demandListBean || demandListBean.getList().size() == 0) {
            mydemandAdapter.setEmptyView(R.layout.layout_nodata_common,recycleView);
        }
        if (tag == 1) {
            mydemandAdapter.setNewData(demandListBean.getList());
        } else {
            mydemandAdapter.addData(demandListBean.getList());
        }

    }

    @Override
    public void deleteDemandSuccess(BaseData data, int i) {
        mydemandAdapter.removeItem(i);

    }

    @Override
    public void getMyRecommendSuccess(MyRecommendBean myRecommendBean) {

    }

    @Override
    public void isInfoSuccess(IsInfoBean isInfoBean) {

    }
}